#include "CreatCircleWidget.h"

#include "Core/MainWindow.h"
#include <vtkProperty.h>
#include <QMessageBox>
#include "FeatureShape/FeatureShape.h"

CreatCircleWidget::CreatCircleWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRWCreateCurveWidget(inMainWindow, inOperation, parent)
	, ui(new Ui::CreatCircleWidgetClass())
{
	ui->setupUi(this);

	connect(ui->confirm, &QPushButton::clicked, this, &CreatCircleWidget::onConfirm);
	connect(ui->cancel, &QPushButton::clicked, this, &CreatCircleWidget::close);
	connect(ui->modify, &QPushButton::clicked, this, &CreatCircleWidget::onApply);

	ui->startSelectWidget->installEventFilter(this);
    ui->tangentSelectWidget->installEventFilter(this);
	ui->endSelectWidget->installEventFilter(this);

	connect(ui->startX, &QDoubleSpinBox::valueChanged, this, &CreatCircleWidget::onPosChanged);
	connect(ui->startY, &QDoubleSpinBox::valueChanged, this, &CreatCircleWidget::onPosChanged);
	connect(ui->startZ, &QDoubleSpinBox::valueChanged, this, &CreatCircleWidget::onPosChanged);
	connect(ui->tangentX, &QDoubleSpinBox::valueChanged, this, &CreatCircleWidget::onPosChanged);
	connect(ui->tangentY, &QDoubleSpinBox::valueChanged, this, &CreatCircleWidget::onPosChanged);
	connect(ui->tangentZ, &QDoubleSpinBox::valueChanged, this, &CreatCircleWidget::onPosChanged);
	connect(ui->endX, &QDoubleSpinBox::valueChanged, this, &CreatCircleWidget::onPosChanged);
	connect(ui->endY, &QDoubleSpinBox::valueChanged, this, &CreatCircleWidget::onPosChanged);
	connect(ui->endZ, &QDoubleSpinBox::valueChanged, this, &CreatCircleWidget::onPosChanged);

	ui->startSelectWidget->setStyleSheet("background-color:rgb(255,178,102)");

	m_startPnt = new RWTripleFloatInput(ui->startX, ui->startY, ui->startZ);
    m_tangentPnt = new RWTripleFloatInput(ui->tangentX, ui->tangentY, ui->tangentZ);
	m_endPnt = new RWTripleFloatInput(ui->endX, ui->endY, ui->endZ);

	m_mainWindow->SetSelectionMode(SM_Vertex);

	// operation -> param
	if (inOperation)
	{
		CreateShapeOperation* CreateOP = static_cast<CreateShapeOperation*>(inOperation);
		FeatureShapeArcCircle* CircleShape = static_cast<FeatureShapeArcCircle*>(CreateOP->m_featureShape.get());
		m_startPnt->SetValue(CircleShape->_start);
        m_tangentPnt->SetValue(CircleShape->_tangent);
		m_endPnt->SetValue(CircleShape->_end);
	}

	cacheActor = m_mainWindow->createArcCircle(m_startPnt->GetValue(), gp_Pnt(m_tangentPnt->GetValue()), gp_Pnt(m_endPnt->GetValue()));
}

CreatCircleWidget::~CreatCircleWidget()
{
	delete ui;
	delete m_startPnt;
	delete m_tangentPnt;
	delete m_endPnt;
}

void CreatCircleWidget::onPosChanged()
{
	if (!cacheActor)
	{
		return;
	}

	// 批量修改
	if (bBatchChange)
	{
		return;
	}

	UpdateInner();
}

bool CreatCircleWidget::eventFilter(QObject* obj, QEvent* event)
{
	QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
	if (event->type() == QEvent::MouseButtonPress && mouseEvent->button() == Qt::LeftButton)
	{
		if (obj == ui->startSelectWidget && focusIndex != 0)
		{
			ui->startSelectWidget->setStyleSheet("background-color:rgb(255,178,102)");
			ui->tangentSelectWidget->setStyleSheet("background-color: #FFFFFF;");
			ui->endSelectWidget->setStyleSheet("background-color: #FFFFFF;");
			focusIndex = 0;
		}
		if (obj == ui->tangentSelectWidget && focusIndex != 1)
		{
			ui->tangentSelectWidget->setStyleSheet("background-color:rgb(255,178,102)");
			ui->startSelectWidget->setStyleSheet("background-color: #FFFFFF;");
			ui->endSelectWidget->setStyleSheet("background-color: #FFFFFF;");
			focusIndex = 1;
		}
		if (obj == ui->endSelectWidget && focusIndex != 2)
		{
			ui->endSelectWidget->setStyleSheet("background-color:rgb(255,178,102)");
			ui->startSelectWidget->setStyleSheet("background-color: #FFFFFF;");
			ui->tangentSelectWidget->setStyleSheet("background-color: #FFFFFF;");
			focusIndex = 2;
		}
	}

	return QObject::eventFilter(obj, event);
}

void CreatCircleWidget::OnPickPosition(const gp_Pnt& inPosition)
{
	bBatchChange = true;
	if (focusIndex == 0)
	{
		m_startPnt->SetValue(inPosition);
	}
	else if (focusIndex == 1)
	{
		m_tangentPnt->SetValue(inPosition);
	}
    else if (focusIndex == 2)
	{
		m_endPnt->SetValue(inPosition);
	}
	bBatchChange = false;

	UpdateInner();
}

ShapeOperation* CreatCircleWidget::GetOperation()
{
	ShapeOperation* op = new CreateShapeOperation(tr("圆曲线"), cacheActor->m_featureShape);
	return op;
}

void CreatCircleWidget::GenerateFeatureShapeForCacheActor()
{
	cacheActor->m_featureShape.reset(new FeatureShapeArcCircle(cacheActor->OccShape(), m_startPnt->GetValue(),
		m_tangentPnt->GetValue(), m_endPnt->GetValue()));
}

void CreatCircleWidget::Reset()
{
	QRWCreateCurveWidget::Reset();

	m_startPnt->SetValue(gp_Pnt());
	m_tangentPnt->SetValue(gp_Pnt(1, 1, 0));
	m_endPnt->SetValue(gp_Pnt(2, 0, 0));

	cacheActor = m_mainWindow->createArcCircle(m_startPnt->GetValue(), gp_Pnt(m_tangentPnt->GetValue()), gp_Pnt(m_endPnt->GetValue()));
	onPosChanged();
}

void CreatCircleWidget::UpdateInner()
{
	gp_Pnt startPoint(ui->startX->value(), ui->startY->value(), ui->startZ->value());
	gp_Pnt tangentPoint(ui->tangentX->value(), ui->tangentY->value(), ui->tangentZ->value());
	gp_Pnt endPoint(ui->endX->value(), ui->endY->value(), ui->endZ->value());

	// 判断是否有重合的点
    if (startPoint.IsEqual(tangentPoint, Precision::Confusion()) || startPoint.IsEqual(endPoint, Precision::Confusion()) || tangentPoint.IsEqual(endPoint, Precision::Confusion()))
	{
        QMessageBox::warning(this, tr("Error"), tr("The start point, tangent point and end point cannot be the same point."));
		return;
	}

	cacheActor = m_mainWindow->UpdateCircleActor(cacheActor, startPoint, tangentPoint, endPoint);
}
